package com.agu.common.tree;

import java.util.ArrayList;
import java.util.List;


public class OrgNode {

	
	private String guid;
	private String name;
	private String parentId;
	private List<OrgNode> children;
	private Integer orgType;   //类别   1 学校 2 院  3 系  5 校级部门 6 院级部门
	
	private String xiaoId;
	private String yuanId;
	private String xiId;
	
	private String idpath;
	private String namepath;
	private Integer level=0;

	public OrgNode() {
		super();
		// TODO Auto-generated constructor stub
	}

	public OrgNode(String guid, String name, String parentId) {
		super();
		this.guid = guid;
		this.name = name;
		this.parentId = parentId;
	}
	
	public OrgNode(String guid, String name, String parentId, Integer orgType) {
		super();
		this.guid = guid;
		this.name = name;
		this.parentId = parentId;
		this.orgType = orgType;
	}


	public OrgNode(String guid, String name) {
		super();
		this.guid = guid;
		this.name = name;
	}

	public OrgNode(String id, String text, List<OrgNode> children) {
		super();
		this.guid = id;
		this.name = text;
		this.children = children;
	}

	public void addChild(OrgNode child) {
		if (children == null) {
			children = new ArrayList<OrgNode>();
		}
		children.add(child);
	}



	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getOrgType() {
		return orgType;
	}

	public void setOrgType(Integer orgType) {
		this.orgType = orgType;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public String getIdpath() {
		return idpath;
	}

	public void setIdpath(String idpath) {
		this.idpath = idpath;
	}

	public String getNamepath() {
		return namepath;
	}

	public void setNamepath(String namepath) {
		this.namepath = namepath;
	}

	public List<OrgNode> getChildren() {
		return children;
	}

	public void setChildren(List<OrgNode> children) {
		this.children = children;
	}
	
	public String getXiaoId() {
		return xiaoId;
	}

	public void setXiaoId(String xiaoId) {
		this.xiaoId = xiaoId;
	}

	public String getYuanId() {
		return yuanId;
	}

	public void setYuanId(String yuanId) {
		this.yuanId = yuanId;
	}

	public String getXiId() {
		return xiId;
	}

	public void setXiId(String xiId) {
		this.xiId = xiId;
	}

	public String getPreName() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < level; i++) {
			sb.append("　");
		}
		sb.append("┣ ");
		sb.append(name);
		return sb.toString();
	}

	/**
	 * 按tree排序
	 * 
	 * @param list
	 * @return
	 */
	public static List<OrgNode> buildTreeList(List<OrgNode> list) {
		List<OrgNode> nodelist = new ArrayList<OrgNode>();
		for (OrgNode node : list) {
			if (node.getParentId() == null || "0".equals(node.getParentId()) || "".equals(node.getParentId())) {
				node.setIdpath(node.guid);
				node.setNamepath(node.name);
				
				if(node.getOrgType().intValue()==1)
				{
					node.setXiaoId(node.getGuid());
				}
				
				if(node.getOrgType().intValue()==2)
				{
					node.setYuanId(node.getGuid());
				}
				
				if(node.getOrgType().intValue()==3)
				{
					node.setXiId(node.getGuid());
				}
				
				List<OrgNode> templist = node.getChildren(list, 1);
				node.setLevel(1);
				nodelist.add(node);
				nodelist.addAll(templist);
			}
		}
		return nodelist;
	}

	public List<OrgNode> getChildren(List<OrgNode> list, int level) {
		List<OrgNode> nodelist = new ArrayList<OrgNode>();
		for (OrgNode node : list) {
			if (node.getParentId()!=null && this.guid.equals(node.getParentId())) {
				node.setLevel(level + 1);

				String idpath = this.idpath +"."+ node.guid;
				String namepath =this.namepath +"."+  node.name;

				node.setIdpath(idpath);
				node.setNamepath(namepath);
				
				if(node.getOrgType().intValue()==1)
				{
					node.setXiaoId(node.getGuid());
				}else {
					node.setXiaoId(this.getXiaoId());
				}
				
				if(node.getOrgType().intValue()==2)
				{
					node.setYuanId(node.getGuid());
				}else {
					node.setYuanId(this.getYuanId());
				}
				
				if(node.getOrgType().intValue()==3)
				{
					node.setXiId(node.getGuid());
				}else{
					node.setXiId(this.getXiId());
				}
				
				
				
				nodelist.add(node);
				List<OrgNode> templist = node.getChildren(list, level + 1);
				nodelist.addAll(templist);
			}
		}
		return nodelist;
	}
	

	/**
	 * 构建树
	 * 
	 * @param list
	 * @return
	 */
	public static List<OrgNode> buildTree(List<OrgNode> list) {
		List<OrgNode> nodelist = new ArrayList<OrgNode>();
		for (OrgNode orgNode : list) {
			
			if (orgNode!=null && (orgNode.getParentId() == null || "0".equals(orgNode.getParentId()) || "".equals(orgNode.getParentId()))) {
				
				System.out.println(orgNode.guid);
				System.out.println(orgNode.name);
				orgNode.setIdpath(orgNode.guid);
				orgNode.setNamepath(orgNode.name);
				orgNode.buildNode(list);
				
				nodelist.add(orgNode);
			}
		}
		return nodelist;
	}

	/**
	 * 构建一个节点
	 * 
	 * @param list
	 */
	public void buildNode(List<OrgNode> list) {
		List<OrgNode> nodelist = new ArrayList<OrgNode>();
		for (OrgNode orgNode : list) {
			if (orgNode.getParentId()!=null && this.guid.equals(orgNode.getParentId())) {
				

				String idpath = this.idpath +"."+ orgNode.guid;
				String namepath =this.namepath +"."+  orgNode.name;
				
				System.out.println(idpath);
				System.out.println(namepath);
				
				orgNode.setIdpath(idpath);
				orgNode.setNamepath(namepath);
				orgNode.buildNode(list);
				nodelist.add(orgNode);
			}
		}
		children = nodelist;
	}
	
	public static void main(String[] args) {
		List<OrgNode> nodelist = new ArrayList<OrgNode>();
		
		OrgNode node1 = new OrgNode("1","深圳大学", "",1);
		OrgNode node2 = new OrgNode("2","文学院", "1",2);
		OrgNode node3 = new OrgNode("3","考古系", "2",3);
		OrgNode node4 = new OrgNode("4","科学院", "1",2);
		OrgNode node5 = new OrgNode("5","数学系", "4",3);
		OrgNode node6 = new OrgNode("6","化学系", "4",3);
		OrgNode node7 = new OrgNode("7","物理系", "4",3);
		OrgNode node8 = new OrgNode("8","办公室", "7",5);
		OrgNode node9 = new OrgNode("9","办公室", "6",5);
		OrgNode node10 = new OrgNode("10","办公室", "1",6);
		
		nodelist.add(node1);
		nodelist.add(node2);
		nodelist.add(node3);
		nodelist.add(node4);
		nodelist.add(node5);
		nodelist.add(node6);
		nodelist.add(node7);
		nodelist.add(node8);
		nodelist.add(node9);
		nodelist.add(node10);
		
		//List<OrgNode> list = OrgNode.buildTree(nodelist);
		
		System.out.println("===================");
		System.out.println("\n\n");
		
		List<OrgNode> nodelist1 = OrgNode.buildTreeList(nodelist);
		
		
		for(OrgNode node: nodelist1) {
			System.out.println(node.getPreName());
			//System.out.println(node.getIdpath());
			//System.out.println(node.getNamepath());
		}
		System.out.println("===================");
		System.out.println("\n\n");
		for(OrgNode node: nodelist1) {
			System.out.println(node.getIdpath());
		}
		System.out.println("===================");
		System.out.println("\n\n");
		for(OrgNode node: nodelist1) {

			System.out.print(node.getNamepath());    
			System.out.print("    --学校:"+node.getXiaoId()); 
			System.out.print("    --学院:"+node.getYuanId());
			System.out.println("    --系:"+node.getXiId());
		}
	}
}
